package com.bot.rf.util;

import com.bot.rf.entity.BookInfo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import static com.bot.rf.util.MyLogger.LOGGER;
public class WebServiceUtil {

    public static String[] LOCATIONS = {
            "01040105A01",
            "01040105A02",
            "01040105A03",
            "01040105A04",
            "01040105A05",
            "01040105A06",
            "01040105A07",
            "01040105A08",
            "01040105A09",
            "01040105A10",
            "01040105A11",
            "01040105A12",
            "01040105A13",
            "01040105A14",
            "01040105A15",
            "01040105B01",
            "01040105B02",
            "01040105B03",
            "01040105B04",
            "01040105B05",
            "01040105B06",
            "01040105B07",
            "01040105B08",
            "01040105B09",
            "01040105B10",
            "01040105B11",
            "01040105B12",
            "01040105B13",
            "01040105B14",
            "01040105B15",
            "01040107A01",
            "01040107A02",
            "01040107A03",
            "01040107A04",
            "01040107A05",
            "01040107A06",
            "01040107A07",
            "01040107A08",
            "01040107A09",
            "01040107A10",
            "01040107A11",
            "01040107A12",
            "01040107A13",
            "01040107A14",
            "01040107A15",
            "01040107B01",
            "01040107B02",
            "01040107B03",
            "01040107B04",
            "01040107B05",
            "01040107B06",
            "01040107B07",
            "01040107B08",
            "01040107B09",
            "01040107B10",
            "01040107B11",
            "01040107B12",
            "01040107B13",
            "01040107B14",
            "01040107B15",
            "01040106A01",
            "01040106A02",
            "01040106A03",
            "01040106A04",
            "01040106A05",
            "01040106A06",
            "01040106A07",
            "01040106A08",
            "01040106A09",
            "01040106A10",
            "01040106A11",
            "01040106A12",
            "01040106B01",
            "01040106B02",
            "01040106B03",
            "01040106B04",
            "01040106B05",
            "01040106B06",
            "01040106B07",
            "01040106B08",
            "01040106B09",
            "01040106B10",
            "01040106B11",
            "01040106B12",

    };

    /**
     * 根据tagId查询图书信息
     * @param tagIds
     * @return
     */
    public static List<BookInfo> updateBookInfoByWebService(List<String> tagIds) {
        List<BookInfo> bookInfos = new ArrayList<>();
        HttpClient httpClient = new DefaultHttpClient();
        String fullUrl = "http://202.120.82.4/Web/WebServiceByszWhere.asmx/GetItemsByszWhere?&szDescribe=&szCaseNO=&szBarCode=&szLocation=&szTID=";
        for (String id:tagIds) {
            String url = fullUrl + id;
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if (null != entity) {
                    InputStream in = entity.getContent();
                    Document document = new SAXReader().read(in);
                    Element rootElement = document.getRootElement();
                    if (rootElement.element("diffgram") != null && rootElement.element("diffgram").element("NewDataSet") != null){
                        List<Element> elementList = rootElement.element("diffgram").element("NewDataSet").elements("Table");
                        if (elementList != null && elementList.size() > 0){
                            Element item = elementList.get(0);
                            BookInfo bookInfo = new BookInfo();
                            bookInfo.setTagId(item.elementText("TID"));
                            bookInfo.setBarcode(item.elementText("BARCODE"));
                            bookInfo.setBookName(item.elementText("TITLE"));
                            bookInfo.setBookIndex(item.elementText("CALLNUMBER"));
                            String status = item.elementText("STATUS");
                            if (status == null || status == ""){
                                status = "0";
                            }
                            bookInfo.setProcessType(status);
                            bookInfo.setTemporaryLocationName(item.elementText("caseno"));
                            bookInfos.add(bookInfo);
                            LOGGER.info("查询成功");
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bookInfos;
    }

    /**
     * 根据查询图书信息
     * @param list
     * @return
     */
    public static List<BookInfo> updateByBarcode(List<BookInfo> list){
        List<BookInfo> bookInfos = new ArrayList<>();
        HttpClient httpClient = new DefaultHttpClient();
        String fullUrl = "http://202.120.82.4/Web/WebServiceByszWhere.asmx/GetItemsByszWhere?&szDescribe=&szCaseNO=&szTID=&szLocation=&szBarCode=";
        for (BookInfo b:list) {
            try {
                String url = fullUrl + b.getBarcode();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if (null != entity) {
                    InputStream in = entity.getContent();
                    Document document = new SAXReader().read(in);
                    Element rootElement = document.getRootElement();
                    if (rootElement.element("diffgram") != null && rootElement.element("diffgram").element("NewDataSet") != null){
                        List<Element> elementList = rootElement.element("diffgram").element("NewDataSet").elements("Table");
                        if (elementList != null && elementList.size() > 0){
                            Element item = elementList.get(0);
                            BookInfo bookInfo = new BookInfo();
//                            bookInfo.setTagId(item.elementText("TID"));
                            bookInfo.setTagId(b.getTagId());
                            bookInfo.setBarcode(item.elementText("BARCODE"));
                            bookInfo.setBookName(item.elementText("TITLE"));
                            bookInfo.setBookIndex(item.elementText("CALLNUMBER"));
                            String status = item.elementText("STATUS");
                            if (status == null || status == ""){
                                status = "0";
                            }
                            bookInfo.setProcessType(status);
                            bookInfo.setTemporaryLocationName(item.elementText("caseno"));
                            bookInfos.add(bookInfo);
                            LOGGER.info("查询成功");
                        }
                    }

                }
            } catch (Exception e) {
                LOGGER.error("异常 " +b.getTagId() +"   " + b.getBarcode() );
                continue;
//                e.printStackTrace();
            }
        }
        return bookInfos;
    }


    /**
     * 根据tagId查询图书信息
     * @return
     */
    public static List<BookInfo> updateBookInfoByLocation() {
        List<BookInfo> bookInfos = new ArrayList<>();
        HttpClient httpClient = new DefaultHttpClient();
        String fullUrl = "http://202.120.82.4/Web/WebServiceByszWhere.asmx/GetItemsByszWhere?&szDescribe=&szTID=&szBarCode=&szLocation=&szCaseNO=";
        for (String location:LOCATIONS) {
            try {
                String url = fullUrl + location;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if (null != entity) {
                    InputStream in = entity.getContent();
                    Document document = new SAXReader().read(in);
                    Element rootElement = document.getRootElement();
                    if (rootElement.element("diffgram") != null && rootElement.element("diffgram").element("NewDataSet") != null){
                        List<Element> elementList = rootElement.element("diffgram").element("NewDataSet").elements("Table");
                        if (elementList != null && elementList.size() > 0){
                            for (Element item : elementList) {
                                BookInfo bookInfo = new BookInfo();
//                                bookInfo.setTagId(item.elementText("TID"));
                                bookInfo.setBarcode(item.elementText("BARCODE"));
                                bookInfo.setBookName(item.elementText("TITLE"));
                                bookInfo.setBookIndex(item.elementText("CALLNUMBER"));
                                String status = item.elementText("STATUS");
                                if (status == null || status == ""){
                                    status = "0";
                                }
                                bookInfo.setProcessType(status);
                                bookInfo.setTemporaryLocationName(item.elementText("caseno"));
                                bookInfos.add(bookInfo);
                            }
                        }
                        LOGGER.info(location + " 查询成功" + "，共" +elementList.size() + "条");
                    }

                }
            } catch (Exception e) {
                continue;
//                e.printStackTrace();
            }
        }
        return bookInfos;
    }


}
